package dictionary;

import java.math.*;
import dictionary.Response;




public class Main {
	

	public static boolean Verify(Response answer)
	{
		if(answer == null)
		{
			return false;
		}
	PallierEncypt pall = answer.pallier;
	BigInteger decrptMsg = pall.Decryption(answer.encryptedmessage);	
	BigInteger decryptRes = pall.Decryption(answer.encryptedresponse);
	Integer dummymsg = answer.dummymsg;
	BigInteger nsquare = answer.n.multiply(answer.n);
	System.out.println("\nDummy : " + dummymsg + "\ndecrptMsg : " + decrptMsg +  "\nnsquare : " + nsquare + "\nDecryptedResponse : " + decryptRes);
	BigInteger newdummymsg = (BigInteger.valueOf(dummymsg)).add(decrptMsg).mod(nsquare);
	if (newdummymsg.equals(decryptRes))
	{
		return true;	
	}
	else 
	{
		return false;	
	}
	}
	
	public static void main(String[] args) 
	{
	/*Integer[] valuesTobeStored=new Integer[5];	
	valuesTobeStored[0]=Integer.valueOf(2);
	valuesTobeStored[1]=Integer.valueOf(5);
	valuesTobeStored[2]=Integer.valueOf(29);
	valuesTobeStored[3]=Integer.valueOf(59);
	valuesTobeStored[4]=Integer.valueOf(60);*/
	Authenticated_Dictionary mydict=new Authenticated_Dictionary();
	
	int[] oldArray = {10,20,30,40,50};
	
	Integer[] newArray = new Integer[oldArray.length];

	//convert int[] to Integer[]
    for(int ctr = 0; ctr <oldArray.length; ctr++) 
    {
	    newArray[ctr] = Integer.valueOf(oldArray[ctr]);
	}
    mydict.createDictionary(newArray);
    mydict.PrintElements();
    mydict.deleteElemenk(10);
    mydict.PrintElements();
    mydict.insertElement(60);
    mydict.PrintElements();
    mydict.Update(30,70);
    mydict.PrintElements();
	Response answer = mydict.queryResponse(20);
	System.out.println("Verifying response received after querying for data = 20");
	boolean resultofVerification = Verify(answer);
	System.out.println("\nResult : " + resultofVerification + "\n");
	
	answer = mydict.queryResponse(30);
	System.out.println("Verifying response received after querying for data = 30");
	resultofVerification = Verify(answer);
	System.out.println("\nResult : " + resultofVerification);
   	}
}
